package com.sdcs.ui;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.sdcs.api.HttpApiAccessor;
import com.sdcs.model.Student;
import com.sdcs.util.Constants;
import com.sdcs.R;

import com.sdcs.component.Connect;

/**
 * 系统启动类，显示操作
 * @author Administrator
 *
 */
public class MainActivity extends Activity {
    private LinearLayout kecheng_row ,  kaoqin_row , exitRow  , two_row , three_row  ;
	private ProgressDialog dialog ;  
	private List<Student> list;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initLayout();
    }

    public void initLayout(){
	   		dialog = new ProgressDialog(this);
			dialog.setMessage("正在加载用户信息...请稍候");
			dialog.show();
			new Thread(){
				public void run() {
					loadAllUserInfo();
					runOnUiThread(new Runnable() {
						public void run() {  
							for (Student student: list) {
								if (student.getStudent_name().equals(Constants.userId )) {
									
									System.out.println("student.getStudent_name() ===" + student.getStudent_name() );
									Constants.student = student;
	 
									load();
								}  
							} 
							dialog.dismiss();
						}
					});
				};
			}.start();  
	}

    private void load(){
	     kecheng_row = (LinearLayout)findViewById(R.id.kecheng_row);
	     kecheng_row.setOnClickListener(new View.OnClickListener() {
	         public void onClick(View v) {
	             Intent all = new Intent(MainActivity.this, CourseListActivity.class);
	             startActivity(all);
	         }
	     });

	     two_row = (LinearLayout)findViewById(R.id.two_row);
	     two_row.setOnClickListener(new View.OnClickListener() {
	         public void onClick(View v) {
	             Intent all = new Intent(MainActivity.this, ResourceListActivity.class);
	             startActivity(all);
	         }
	     });

	     three_row = (LinearLayout)findViewById(R.id.three_row);
	     three_row.setOnClickListener(new View.OnClickListener() {
	         public void onClick(View v) {
	             Intent all = new Intent( MainActivity.this, HomeworkListActivity.class);
	             startActivity(all);
	         }
	     });

	     kaoqin_row = (LinearLayout)findViewById(R.id.kaoqin_row);
	     kaoqin_row.setOnClickListener(new View.OnClickListener() {
	         public void onClick(View v) {
	    		 
				 HashMap  map  = new HashMap() ;

				 map.put("student_id",  String.valueOf(Constants.student.getStudent_id()  ) ) ;
				 map.put("student_name", Constants.student.getStudent_name() ) ;
				 
				 String result =  HttpApiAccessor.sendAttendance( map ) ;
				 if( result != null && result.equals("1") ){
					 Toast.makeText(MainActivity.this, "考勤签到成功" , Toast.LENGTH_SHORT ).show() ;
				 }
	         }
	     });
	//     退出
	     exitRow = (LinearLayout)findViewById(R.id.exit_row);
	     exitRow.setOnClickListener(new View.OnClickListener() {
	         public void onClick(View v) {
	             finish();
	         }
	     }); 
    }
	
	/**
	 * 加载所有用户的信息
	 */
	private void loadAllUserInfo() {
		list = new ArrayList<Student>();
		Connect connect = new Connect(Constants.SERVER_USERS, Constants.HTTP_POST);
		try {
			byte[] data = connect.queryServer(null);
			JSONObject object = new JSONObject(new String(data, "gb2312"));
			JSONArray studentArray = object.getJSONArray("students");
			for (int i = 0;i < studentArray.length();i++) {
				JSONObject userObject = studentArray.getJSONObject(i);
				Student student = new Student();
				student.setStudent_id(userObject.getInt("student_id"));
				student.setPassword(userObject.getString("password"));
				student.setStudent_name(userObject.getString("student_name"));
				student.setStudent_screen_name(userObject.getString("student_screen_name"));
				student.setStudent_address(userObject.getString("student_address"));
				student.setStudent_phone(userObject.getString("student_phone"));
				list.add(student);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


}
